/*
 * Created by JFormDesigner on Fri Jun 04 21:34:29 CST 2021
 */

package com.J1ay.carsys.gui.Emp;


import java.awt.event.*;

import com.J1ay.carsys.dao.BowerMapper;
import com.J1ay.carsys.gui.Emp.Dialog.AddBower;
import com.J1ay.carsys.gui.Emp.Dialog.Del;
import com.J1ay.carsys.gui.Emp.Dialog.UpdateBower;
import com.J1ay.carsys.gui.Welcome;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;
import com.J1ay.carsys.utils.MybatisUtils;

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;


/**
 * @author Brainrain
 */
public class Manage extends JFrame {
    Bower[] bowerList;
    int len =0;
    EmpService empService = new EmpServiceImpl();
    int EmpId;
    String Name;
    public Manage(int empId,String name) {
        initComponents();
        this.empId.setText(""+empId);
        this.empName.setText(name);
        EmpId = empId;
        Name = name;
    }

//    表格数据
    public void setBoweTable(){
        bowerList = (Bower[]) empService.getBowerList().getData();
        String[] data = new String[bowerList.length];
        for (int i = 0 ;i < bowerList.length;++i){
            data[i] = bowerList[i].toString();
        }
        this.list1.setListData(data);
        len = MybatisUtils.getSqlSession().getMapper(BowerMapper.class).countBower();
    }

    public void refresh(){
        list1 = new JList();
        this.setBoweTable();
        scrollPane1.setViewportView(list1);
    }
    public void start() {
        this.setVisible(true);
        this.setBoweTable();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void addBowerActionPerformed(ActionEvent e) {
        // TODO add your code here
        new AddBower(this,this.EmpId,len).start();
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Welcome().start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

//    删除
    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.list1.getSelectedIndex();
        if (index == -1) {
            new Warning(this,"您还未选中想要删除的租车人！").start();
        }
        else {
            Bower nowBower = bowerList[index];
            new Del(this,nowBower).start();
            System.out.println(nowBower);
            this.refresh();
        }

    }
//修改
    private void button3ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.list1.getSelectedIndex();
        if (index == -1) {
            new Warning(this,"您还未选中想要修改的车辆！").start();
        }
        else {
           Bower updatebower = bowerList[index];
            new UpdateBower(this,updatebower).start();
            System.out.println(updatebower);
            this.refresh();
        }
    }

    private void button4ActionPerformed(ActionEvent e) {
        // TODO add your code here
        System.out.println(EmpId);
        new Main(EmpId,Name).start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        button2 = new JButton();
        button3 = new JButton();
        addBower = new JButton();
        panel3 = new JPanel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        scrollPane1 = new JScrollPane();
        list1 = new JList();
        empName = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        empId = new JLabel();
        button1 = new JButton();
        button4 = new JButton();

        //======== this ========
        setBackground(Color.white);
        setForeground(SystemColor.windowText);
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u76ee\u524d\u6240\u5728:\u4eba\u5458\u7ba1\u7406");

        //======== panel1 ========
        {
            panel1.setBackground(Color.lightGray);

            //======== panel2 ========
            {
                panel2.setBackground(new Color(211, 203, 199));

                //---- button2 ----
                button2.setText("\u5220\u9664");
                button2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button2ActionPerformed(e);
                    }
                });

                //---- button3 ----
                button3.setText("\u4fee\u6539");
                button3.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button3ActionPerformed(e);
                    }
                });

                GroupLayout panel2Layout = new GroupLayout(panel2);
                panel2.setLayout(panel2Layout);
                panel2Layout.setHorizontalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                            .addGap(0, 2, Short.MAX_VALUE)
                            .addGroup(panel2Layout.createParallelGroup()
                                .addComponent(button2)
                                .addComponent(button3))
                            .addContainerGap())
                );
                panel2Layout.setVerticalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGap(79, 79, 79)
                            .addComponent(button2)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                            .addComponent(button3)
                            .addGap(65, 65, 65))
                );
            }

            //---- addBower ----
            addBower.setText("\u65b0\u589e\u4eba\u5458");
            addBower.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            addBower.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    addBowerActionPerformed(e);
                }
            });

            //======== panel3 ========
            {

                //---- label4 ----
                label4.setText("\u8d26\u53f7");

                //---- label5 ----
                label5.setText("\u8eab\u4efd\u8bc1");

                //---- label6 ----
                label6.setText("\u540d\u5b57");

                //---- label7 ----
                label7.setText("\u7535\u8bdd");

                //---- label8 ----
                label8.setText("\u7b49\u7ea7");

                //---- label9 ----
                label9.setText("\u6ce8\u518c\u65f6\u95f4");

                GroupLayout panel3Layout = new GroupLayout(panel3);
                panel3.setLayout(panel3Layout);
                panel3Layout.setHorizontalGroup(
                    panel3Layout.createParallelGroup()
                        .addGroup(panel3Layout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(label4, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                            .addGap(38, 38, 38)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addGap(33, 33, 33)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(label8, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label9, GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                );
                panel3Layout.setVerticalGroup(
                    panel3Layout.createParallelGroup()
                        .addGroup(panel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label4, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label8, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label9, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
                );
            }

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(list1);
            }

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addComponent(panel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE))
                                .addGap(15, 15, 15)
                                .addComponent(panel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                                .addGap(0, 518, Short.MAX_VALUE)
                                .addComponent(addBower, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(addBower)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(panel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE))
                            .addComponent(panel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
            );
        }

        //---- label2 ----
        label2.setText("\u6b22\u8fce\u60a8~");

        //---- label3 ----
        label3.setText("\u5de5\u53f7\uff1a");

        //---- button1 ----
        button1.setText("\u9000\u51fa");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button1ActionPerformed(e);
            }
        });

        //---- button4 ----
        button4.setText("\u9996\u9875");
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button4ActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPaneLayout.setHonorsVisibility(false);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(button4)
                    .addGap(35, 35, 35)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label3)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(empId, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
                    .addGap(3, 3, 3)
                    .addComponent(empName, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(label2, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(button1)
                    .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label2)
                            .addComponent(empName)
                            .addComponent(label3)
                            .addComponent(empId)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
                        .addComponent(button4, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JPanel panel1;
    private JPanel panel2;
    private JButton button2;
    private JButton button3;
    private JButton addBower;
    private JPanel panel3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JScrollPane scrollPane1;
    private JList list1;
    private JLabel empName;
    private JLabel label2;
    private JLabel label3;
    private JLabel empId;
    private JButton button1;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
